<template>
  <div>
    <base-right-dialog
      ref="baseRightDialog"
      :footerShow="true"
      :dialogVisible.sync="dialogVisible"
      :title="dialogTitle + ' 集成产品清单'"
      @handleClose="handleDialogClose"
      :type="dialogType"
      :submitShow="submitShow"
      @handleConfirmClick="handleConfirmClick"
    >
      <base-form
        ref="basicsForm"
        :formRow="formRow"
        :isFunBtn="false"
        :rules="basicsRules"
        class="dialog_form"
        :spanWidth="`120px`"
        :loading="vLoading"
      ></base-form>
      <el-button
        style="width: 125px; margin: 20px 0"
        @click="addVersionDialog"
        icon="el-icon-plus"
        :disabled="submitShow ? false : true"
        >添加版本</el-button
      >
      <base-table
        :border="true"
        :showIndex="true"
        :tableColumn="tableVersionColumn"
        :tableData="tableVersionData"
        :funWidth="80"
        :funData="funData"
        @onFunc="onFunc"
      >
        <template v-slot:versionNumber="{ row }">
          <div style="width: 100%">
            <el-input
              v-model="row.versionNumber"
              clearable
              type="text"
              :disabled="!submitShow || row.disabled"
            ></el-input>
          </div>
        </template>
        <template v-slot:versionDescription="{ row }">
          <div style="width: 100%">
            <el-input
              v-model="row.versionDescription"
              clearable
              type="text"
              :disabled="!submitShow"
            ></el-input>
          </div>
        </template>
      </base-table>
    </base-right-dialog>
  </div>
</template>

<script>
import baseRightDialog from "@/components/base/baseRightDialog";
import baseForm from "@/components/base/baseNewForm";
import baseTable from "@/components/base/baseTable";
import configData from "./configData";
import { authApi } from "@/api/apis/auth";
export default {
  components: {
    baseRightDialog,
    baseForm,
    baseTable,
  },
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "",
      dialogType: "",
      formRow: configData.formRow,
      basicsRules: configData.basicsRules,
      vLoading: false,
      newMarryOptions: [],
      submitShow: true,
      loadingType: true,
      tableVersionData: [],
      tableVersionColumn: configData.tableVersionColumn,
      funData: [],
      isEdit: false
    };
  },
  mounted() {},
  methods: {
    openDialog(type, row) {
      this.queryProductClassfy();
      this.formRow = configData.formRow;
        this.submitShow = true;
      this.isEdit = false
      this.funData = [
        {
          color: "#ff0000",
          text: "删除",
        }
      ]
      // 新增
      if (type == "add") {
        this.dialogTitle = "新增";
        this.dialogType = "add";
      }
      // 编辑
      if (type == "edit") {
        this.isEdit = true
        this.dialogTitle = "编辑";
        this.dialogType = "edit";
        this.productGetById(row.id);
      }
      // 查看
      if (type == "show") {
        this.funData = Object.assign([],[])
        this.submitShow = false;
        this.formRow = configData.formRowShow;
        this.dialogTitle = "查看";
        this.dialogType = "show";
        this.productGetById(row.id);

      }
      this.dialogVisible = true;
    },
    // 编辑详情
    async productGetById(id) {
      let params = {
        id: id,
      };
      let res = await authApi(
        "sysProductService",
        "product",
        "queryProduct",
        "",
        params
      );
      if (res.status == "200") {
        this.$nextTick(() => {
          this.$refs.basicsForm.incomingParameters(res.attribute);
          let result = [];
          if (res.attribute.sysProductVersionList.length > 0) {
            res.attribute.sysProductVersionList.forEach((item) => {
              let obj = {
                id: item.id,
                versionNumber: item.versionNumber,
                versionDescription: item.versionDescription,
                disabled:true
              };
              result.push(obj);
            });
          }
          this.tableVersionData = Object.assign([], result);
        });
      }
    },
    addVersionDialog() {
      let obj = {
        id: new Date().getTime().toString(),
        versionNumber: "",
        versionDescription: "",
        disabled:false
      };
      this.tableVersionData.push(obj);
    },
    // 删除
    onFunc(index, row) {
      if (index == 0) {
        this.$delConfirm().then(() => {
          this.tableVersionData.forEach((item, itemIndex) => {
            if (item.id === row.id) {
              this.tableVersionData.splice(itemIndex, 1);
            }
          });
        });
      }
    },
    // 弹窗关闭
    handleDialogClose() {
      this.tableVersionData = Object.assign([], []);
      this.$refs.basicsForm.resetFields();
      this.dialogVisible = false;
    },
    // 弹窗确认按钮
    handleConfirmClick() {
      this.$refs.basicsForm.$refs["ruleForm"].validate((valid) => {
        if (!valid) {
          return;
        } else {
          let params = {
            ...this.$refs.basicsForm.ruleForm,
            sysProductVersionList: [],
          };
          let result = [];
          if (this.tableVersionData.length > 0) {
            this.tableVersionData.forEach((item) => {
              let obj = {
                versionNumber: item.versionNumber,
                versionDescription: item.versionDescription,
              };
              result.push(obj);
            });
          }
          params.sysProductVersionList = Object.assign([], result);
          if (this.dialogType == "add") {
            this.openLoading("submit");
            this.productSaveDto(params);
          }
          if (this.dialogType == "edit") {
            this.openLoading("submit");
            this.productUpdateDto(params);
          }
        }
      });
    },
    // 新增保存
    async productSaveDto(params) {
      let res = await authApi(
        "sysProductService",
        "product",
        "addProduct",
        "",
        params
      );
      if (res.status == "200") {
        this.handleDialogClose();
        this.$vmNews("新增成功", "success");
        this.$emit("resetTable");
      }
    },
    // 编辑保存
    async productUpdateDto(params) {
      let res = await authApi(
        "sysProductService",
        "product",
        "updateProduct",
        "",
        params
      );
      if (res.status == "200") {
        this.handleDialogClose();
        this.$vmNews("更新成功", "success");
        this.$emit("resetTable");
      }
    },
    // 获取所有产品分类
    async queryProductClassfy() {
      let params = {
        tab_name: "sys_product",
        column_name: "classify",
      };
      let res = await authApi(
        "generalServiceImpl",
        "dictionaryshop",
        "selectDictionaryshop",
        "",
        params
      );
      if (res.status == "200") {
        this.formRow[1].elCol[0].options = res.attribute;
      }
    },
  },
};
</script>

<style scoped lang="scss">
.dialogList {
  padding: 16px 0;
  border-top: 1px solid #dcdfe6;
  display: flex;
  flex-direction: column;
}

.updateBtn {
  border: 1px solid #ebedf1;
  padding: 5px 0;
  border-radius: 3px;
  text-align: center;
  font-size: 14px;
  cursor: pointer;
  width: 100px;
}

.updateBtn:hover {
  color: #1890ff;
  border-color: #badeff;
  background-color: #e8f4ff;
}

::v-deep .el-table__body-wrapper.is-scrolling-none {
  height: auto !important;
}

::v-deep .app-container {
  height: auto !important;
}
</style>